﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Rodunu.Business.Interfaces;
using Rodunu.Business.Logic;
using Rodunu.Clients.WPF.Collection;
using Rodunu.Clients.WPF.Controllers;
using Rodunu.Clients.WPF.Settings;

namespace Rodunu.Clients.WPF
{
	/// <summary>
	/// Interaction logic for Main.xaml
	/// </summary>
	public partial class Main : Window
	{
	    private readonly LoginController _loginController;
	    private readonly IManageSettings _manageSettings;

	    #region Constructors

	    public Main()
	    {
	        Name = "MainForm";

	        InitializeComponent();

            _manageSettings = new ManageSettings();

            _loginController = new LoginController(_manageSettings);
            LoginControl.Run(_loginController);
            
	        InitCustomSetup();

	        // Insert code required on object creation below this point.
	        SetControlEvents();
	    }

	    #endregion

        #region Private Methods

        private void SetControlVisible(UIElement element)
        {
            MainHolder.Children.Clear();
            MainHolder.Children.Add(element);
        }

        private void InitCustomSetup()
        {
            RestoreButton.Visibility = Visibility.Collapsed;
            MaximizeButton.Visibility = Visibility.Collapsed;
        }

        private void SetControlEvents()
        {
            SizeChanged += Main_SizeChanged;

            MinimizeButton.Click += Minimize;
            RestoreButton.Click += RestoreButton_Click;
            CloseButton.Click += CloseButton_Click;
            MaximizeButton.Click += MaximizeButton_Click;

            MouseDown += Main_MouseDown;
            SearchBox.GotFocus += SearchBox_GotFocus;
            SearchBox.LostFocus += SearchBox_LostFocus;

            SearchBox.KeyDown += SearchBox_KeyDown;

            Loaded += Main_Loaded;

            CollectionButton.Click += CollectionButton_Click;

            LoginControl.LogUserOn += LoginControl_LogUserOn;
            UserSettingsButton.Click += UserSettingsButton_Click;
        }

        #endregion

	    #region Control Events

        void UserSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            SetControlVisible(new UserSettings(_manageSettings));
        }

        void Main_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //MainHolder.Width = Width;
        }

        void LoginControl_LogUserOn(object sender, EventArgs e)
        {
            LogonCreate logonCreate = new LogonCreate();
            logonCreate.UserCreated += logonCreate_UserCreated;
            logonCreate.UserLoggedOn += logonCreate_UserLoggedOn;
            SetControlVisible(logonCreate);
        }

        void logonCreate_UserLoggedOn(object sender, LogOnEventArgs e)
        {
            if (_loginController.Login(e.LoggedOnUser.UserName, e.LoggedOnUser.Password, e.RememberMe, e.RememberPassword, e.AutoLogIn))
            {
                LoginControl.CurrentUserName = e.LoggedOnUser.UserName;
            }
            else
            {
                MessageBox.Show("Could not log user on.");
            }
        }

        void logonCreate_UserCreated(object sender, LogOnEventArgs e)
        {
            if (_loginController.CreateUser(e.LoggedOnUser, e.RememberMe, e.RememberPassword, e.AutoLogIn))
            {
                LoginControl.CurrentUserName = e.LoggedOnUser.UserName;
                MessageBox.Show(e.LoggedOnUser.UserName + " account created successfully");
            }
            else
            {
                MessageBox.Show("Could not create user");
            }
        }

        void SearchBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (SearchBox.Text.Length > 0 && SearchBox.Text.ToLower() != "search")
                {
                    var searchResults = new SearchResults(new CollectionBL(_manageSettings), _manageSettings);
                    searchResults.Search(SearchBox.Text);
                    SetControlVisible(searchResults);
                }
            }
        }

        void CollectionButton_Click(object sender, RoutedEventArgs e)
        {
            SetControlVisible(new CollectionMain(_manageSettings));
        }

	    void Main_Loaded(object sender, RoutedEventArgs e)
	    {
	        if (WindowState == WindowState.Maximized)
	        {
	            RestoreButton.Visibility = Visibility.Visible;
	            MaximizeButton.Visibility = Visibility.Collapsed;
	        }
	        else
	        {
	            RestoreButton.Visibility = Visibility.Collapsed;
	            MaximizeButton.Visibility = Visibility.Visible;
	        }
	    }
        
	    void MaximizeButton_Click(object sender, RoutedEventArgs e)
	    {
	        WindowState = WindowState.Maximized;
	        Main_Loaded(sender, e);
	    }

	    void SearchBox_LostFocus(object sender, RoutedEventArgs e)
	    {
	        if (SearchBox.Text.Length == 0)
	            SearchBox.Text = "Search";
	    }

	    void SearchBox_GotFocus(object sender, RoutedEventArgs e)
	    {
	        if (SearchBox.Text == "Search")
	            SearchBox.Text = string.Empty;
	    }

	    void Main_MouseDown(object sender, MouseButtonEventArgs e)
	    {
	        if (e.LeftButton == MouseButtonState.Pressed)
	        {
	            DragMove();
	        }
	    }
        
	    void CloseButton_Click(object sender, RoutedEventArgs e)
	    {
	        Close();
	    }

	    void RestoreButton_Click(object sender, RoutedEventArgs e)
	    {
	        WindowState = WindowState.Normal;
	        Main_Loaded(sender, e);
	    }

	    protected void Minimize(object sender, EventArgs e)
	    {
	        WindowState = WindowState.Minimized;
	    }

	    #endregion
    }
}